package com.zcs.app.lucky.ui

import android.view.View
import androidx.lifecycle.lifecycleScope
import cn.sxw.android.base.kt.BaseViewModel
import cn.sxw.android.base.utils.GlideStaticUtils
import cn.sxw.android.base.utils.RandomNameProvider
import com.alibaba.fastjson.JSON
import com.zcs.app.lucky.R
import com.zcs.app.lucky.base.BaseLuckyActivity
import com.zcs.app.lucky.databinding.ActivityPopoBinding
import com.zcs.app.lucky.databinding.ItemLuckyAvatarBinding
import com.zcs.app.lucky.entity.AVATAR_LIST
import com.zcs.app.lucky.entity.LuckyPeople
import com.zcs.app.lucky.entity.TYPE_USER_NORMAL
import com.zcs.app.lucky.helper.AnimatorHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class PopoActivity : BaseLuckyActivity<ActivityPopoBinding, BaseViewModel>() {
    private val canvasWidth by lazy { binding.avatarCanvas.width }
    private val canvasHeight by lazy { binding.avatarCanvas.height }
    private val radius by lazy { binding.space.width / 2f }
    private val maxX by lazy { canvasWidth - radius * 2f }
    private val maxY by lazy { canvasHeight - radius * 2f }

    // 懒加载机制
    override fun onReady() {
        binding.toolbar.tvToolbarTitle.text = "Title Here"
        binding.toolbar.tvToolbarRight.text = "菜单"
    }

    override fun onInit() {
        initView()
        initObserve()
        initData()
    }

    private var avatarList = ArrayList<View>()
    override fun initView() {
        for (i in 1..100) {
            val user = LuckyPeople(TYPE_USER_NORMAL)
            user.name = RandomNameProvider.getName()
            user.avatar = AVATAR_LIST.random()
            user.rank = i

            val avatar = ItemLuckyAvatarBinding.inflate(layoutInflater)
            // 加载头像
            GlideStaticUtils.displayCircleImage(
                user.avatar, R.mipmap.ic_launcher_round, avatar.ivAvatar
            )
            avatar.tvName.text = user.name
            avatar.root.tag = JSON.toJSONString(user)
            avatarList.add(avatar.root)
        }
        startRunning()
    }

    private fun startRunning() {
        lifecycleScope.launch(Dispatchers.Main) {
            for (view in avatarList) {
                delay(200)
                // 添加到Canvas
                binding.avatarCanvas.addView(view)
                view.translationX = Random.nextInt(maxX.toInt()).toFloat()
                view.translationY = Random.nextInt(maxY.toInt()).toFloat()
                startAnim(view)
            }
        }
    }

    private var isRunning = false
    private fun startAnim(v: View) {
        isRunning = true
        lifecycleScope.launch(Dispatchers.Main) {
            delay(666)
            startTranslation(v, AnimatorHelper.randomSpeed(), AnimatorHelper.randomSpeed())
        }
    }

    private fun startTranslation(v: View, speedX: Float, speedY: Float) {
        if (!isRunning) return
        var sx = speedX
        var x1 = v.translationX + speedX
        if (x1 > maxX) {
            x1 = maxX
            sx = AnimatorHelper.calcSpeed(sx)
        } else if (x1 < 0) {
            x1 = 0f
            sx = AnimatorHelper.calcSpeed(sx)
        }
        v.translationX = x1

        var y1 = v.translationY + speedY
        var sy = speedY
        if (y1 > maxY) {
            y1 = maxY
            sy = AnimatorHelper.calcSpeed(sy)
        } else if (y1 < 0) {
            y1 = 0f
            sy = AnimatorHelper.calcSpeed(sy)
        }
        v.translationY = y1
        lifecycleScope.launch(Dispatchers.Main) {
            delay(AnimatorHelper.ANIM_DELAY)
            startTranslation(v, sx, sy)
        }
    }

    override fun initObserve() {
    }

    override fun initData() {
        // hide loading last
        hideLoading()
    }


    override fun addOnClickListener() {
        binding.toolbar.ivToolbarBack.setOnClickListener { onBackPressed() }
    }
}